<?php
/**
 * Created by PhpStorm.FUN-AC-0020-BD建设银行五折购
 * User: allen.qiang
 * Date: 14-11-20
 * Time: 上午10:50
 */
quanli_require('module/base/activity/activityConfig.class.php');

class activityCbc {

    /**
     * 得到活动的信息
     * @return $activity_array
     * @author  20141120 allen.qiang <allen.qiang@fistforward.com>
     */
    public function activity_master(&$arg_array){
        global $G_SHOP;

        //实例化配置类
        $activity_config = new activityConfig('CBC'); //建行信用卡五折购活动配置
        $config_array = $activity_config->config;

        $isbool=false;
        //sql字符串
        $sql = ' ';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();
        //会员名
        if (isset($arg_array['custID']) && is_numeric($arg_array['custID'])) {
            $sql .= ' AND custID = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['custID']);
            $isbool=true;
        }

        //会员ID
        if (isset($arg_array['pcustID']) && !empty($arg_array['pcustID'])) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['pcustID'];
            $isbool=true;
        }
        //促销渠道ID
        if (isset($config_array['billPromotion']) && !empty($config_array['billPromotion'])) {
            $sql .= ' AND billPromotion=?';
            $params_type .= 's';
            $params_array[] = $config_array['billPromotion'];
        }
        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<得到建行信用卡五折购使用状态 0：未使用，1：已使用>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        $status=0;
       if($isbool){
           $sql_master="SELECT COUNT(1) AS item_count FROM `bill_master` WHERE bill_status<>10 AND createTime>'".date('Y-m-d 00:00:00')."' AND createTime<'".date('Y-m-d 23:59:59')."' ".$sql;
           $temp_item_master = &$G_SHOP->DBCA->stmt_query($sql_master,$params_type,$params_array);
           $item_count = $temp_item_master[0]->item_count;
           if($item_count>0){ //已使用建行信用卡五折购
               $status=1;
           }
       }

        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<得到活动的每日蛋糕数量>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        $sql_master2="SELECT COUNT(1) AS item_count FROM `bill_master` WHERE bill_status<>10 AND createTime>'".date('Y-m-d 00:00:00')."' AND createTime<'".date('Y-m-d 23:59:59')."' AND billPromotion=".$config_array['billPromotion'];
        $temp_item_master2 = &$G_SHOP->DBCA->stmt_query($sql_master2);
        $sql_master3="SELECT COUNT(1) AS item_count FROM `bill_master` WHERE bill_status<>10 AND createTime>'".date('Y-m-d 00:00:00',strtotime('-1 day'))."' AND createTime<'".date('Y-m-d 23:59:59',strtotime('-1 day'))."' AND billPromotion=".$config_array['billPromotion'];
        $temp_item_master3 = &$G_SHOP->DBCA->stmt_query($sql_master3);
        $number=$config_array['cake_sum'];
        if($temp_item_master3[0]->item_count>$number){
            $number =$number- ($temp_item_master3[0]->item_count-$number);
        }
        if($temp_item_master2){
            $number =$number- $temp_item_master2[0]->item_count;
        }

        if($number<0)
            $number=0;

        //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<是否在活动时间内 0：未在活动时间内；1：在活动时间内>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
        $isDate=0;
        if(strstr($config_array["activity_Week"],date('w')) && date('Y-m-d H:i:s')<=$config_array["endTime"] && date('Y-m-d H:i:s')>=date('Y-m-d 10:00:00') && date('Y-m-d H:i:s')<=date('Y-m-d 20:00:00')){
            $isDate=1;
        }

        //用户信息数组
        $activity_array = array(
            'billPromotion' => $config_array['billPromotion'],//促销渠道ID
            'activity_status'	     => $status,//使用状态
            'endtime'	     => $config_array['endTime'],//活动结束时间
            'activity_num' => $number,//每日蛋糕所剩数量
            'isDate'        => $isDate,//是否在活动时间内
            'send_end_time' => $config_array["send_end_time"], //配送结束时间
            'activity_Week' => $config_array["activity_Week"], //活动的订购星期
            'paymentID_CBC' =>$config_array["paymentID_CBC"] //建设银行信用卡货到刷卡paymentID
        );

        return $activity_array;

    }


    /**
     * 建行信用卡五折购信息收集
     * @author  20141120 allen.qiang <allen.qiang@fistforward.com>
     */
    public  function activity_cbc($arg_array) {
        global $G_SHOP;

        //取值
        $custID=$arg_array["custID"]; //会员名
        $pcustID=$arg_array["pcustID"]; //会员ID

        try{
            $G_SHOP->DBCA->beginTransaction();

            //保存试建行信用卡五折购信息收集表数据
            $activity_cbc = new entity('activity_cbc');
            $activity_cbc->pcustID = $pcustID;
            $activity_cbc->cbc_date = date("Y-m-d");
            $G_SHOP->DBCA->fetch($activity_cbc);
            if (isset($arg_array['bill_no']) && !empty($arg_array['bill_no'])) {
                $activity_cbc->bill_no = $arg_array['bill_no']; //订单号
                $activity_cbc->bill_code = $arg_array['bill_code']; //订单编号
                $activity_cbc->codeState = 1; //订单状态 0:未下单,1:已下单
                //修改订单表的配送备注
                $bill_master = new entity('bill_master');
                $bill_master->bill_no = $arg_array['bill_no'];
                if ($G_SHOP->DBCA->fetch($bill_master)){
                    $bill_master->remark .= " CCB".$activity_cbc->cbc_code;
                    $G_SHOP->DBCA->save($bill_master);
                }

            }else{
                $activity_cbc->creator = $custID;
                $activity_cbc->createTime = date('Y-m-d H:i:s');
                $activity_cbc->custID = $custID;
                $activity_cbc->product_code = $arg_array["product_code"];//商品编码
                $activity_cbc->goods_name = $arg_array["goods_name"];//商品名
                $activity_cbc->goods_spec = $arg_array["goods_spec"];//商品规格
                $activity_cbc->cbc_code = $arg_array["cbc_code"];//建设银行信用卡卡号后四位
            }

            if (!$G_SHOP->DBCA->save($activity_cbc)) {
                throw new Exception('页面已过期，请刷新页面重试', 404);
            }
            $G_SHOP->DBCA->commit();
            return array( 'code' => 200, 'message' => '保存成功' );
        }
        catch (Exception $e) {

            $G_SHOP->DBCA->rollBack();

            if ($e->getCode() > 0)
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            else {
                error_log($e->getMessage());
                return array( 'code' => 700, 'message' => '系统错误' );
            }
        }


    }


} 